//
//  ChatCurrentListController.m
//  MyChatDemo
//
//  Created by DragonYao on 2017/9/14.
//  Copyright © 2017年 DragonYao. All rights reserved.
//

#import "ChatCurrentListController.h"
#import "OSSocketController.h"

@interface ChatCurrentListController ()
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *indicatorView;

@end

@implementation ChatCurrentListController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kUserLoginStatusNotification object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginStatus:) name:kUserLoginStatusNotification object:nil];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"OS socket test" style:(UIBarButtonItemStylePlain) target:self action:@selector(showOSSocketView:)];
}

- (void)showOSSocketView:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    OSSocketController *socketVC = [storyboard instantiateViewControllerWithIdentifier:@"OSSocketController"];
    socketVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:socketVC animated:YES];
}

- (void)loginStatus:(NSNotification *)noti {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        NSInteger loginStatus = [[noti.userInfo valueForKey:@"LoginStatus"] integerValue];
//        SLog(@"LoginStatus = %ld", loginStatus);
        switch (loginStatus) {
            case XMPPResultTypeLoginConnecting:
                [self.indicatorView startAnimating];
                break;
            case XMPPResultTypeLoginSuccess:
                [self.indicatorView stopAnimating];
                break;
            case XMPPResultTypeLoginFailure:{
                [self.indicatorView stopAnimating];
                [ProgressHUD showError:@"自动登录失败!"];
            }
                break;
            case XMPPResultTypeNetError:{
                [self.indicatorView stopAnimating];
                [ProgressHUD showError:@"网络出现问题！"];
            }
                break;
            default:
                break;
        }
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
